﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WHC.Hotel.BLL;
using WHC.OrderWater.Commons;
using WHC.Security.Entity;

namespace WHC.Hotel.UI.Report
{
    public partial class TeaTopTradeReport : BaseForm
    {
        private List<string> SelectItemList = new List<string>();//统计项目

        public TeaTopTradeReport()
        {
            InitializeComponent();
        }

        private void InitDictItem()
        {
            this.txtCreator.Items.Clear();
            List<UserInfo> userList = WHC.Security.BLL.BLLFactory<WHC.Security.BLL.User>.Instance.GetAll();
            foreach (UserInfo info in userList)
            {
                this.txtCreator.Items.Add(info.FullName);
            }

            this.txtStatisticItem.Items.Clear();
            List<string> itemList = BLLFactory<TeaConsumerList>.Instance.GetGoodsType();
            this.txtStatisticItem.Items.AddRange(itemList.ToArray());
        }

        private void KFTopTradeReport_Load(object sender, EventArgs e)
        {
            InitDictItem();

            this.winGridView1.ProgressBar = this.toolStripProgressBar1.ProgressBar;
            this.winGridView1.AppendedMenu = this.contextMenuStrip1;

            this.winGridView2.ProgressBar = this.toolStripProgressBar1.ProgressBar;
            this.winGridView2.AppendedMenu = this.contextMenuStrip2;

            this.dtStart.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));
            this.dtEnd.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 23:59:59"));
            this.dtOpStart.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));
            this.dtOpEnd.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 23:59:59"));

        }

        private void BindItemCateData()
        {
            #region 添加别名解析
            this.winGridView1.AddColumnAlias("Rank", "名次");
            this.winGridView1.AddColumnAlias("ItemName", "项目名称");
            this.winGridView1.AddColumnAlias("Price", "商品单价");
            this.winGridView1.AddColumnAlias("Quantity", "销售数量");
            this.winGridView1.AddColumnAlias("OriginalAmount", "销售总额");
            this.winGridView1.AddColumnAlias("OfferMoney", "优惠金额");
            this.winGridView1.AddColumnAlias("Amount", "折后金额");
            #endregion

            #region 条件检索

            SearchCondition condition = new SearchCondition();
            condition.AddCondition("ItemType", this.txtStatisticItem.Text, SqlOperator.Like)
            .AddCondition("BeginTime", this.dtStart.Value.ToString(), SqlOperator.MoreThanOrEqual)
            .AddCondition("BeginTime", this.dtEnd.Value.ToString(), SqlOperator.LessThanOrEqual);
            string filter = condition.BuildConditionSql(DatabaseType.SqlServer);

            int topCount = Convert.ToInt32(this.txtTopCount.Text);
            #endregion

            DataTable dt = BLLFactory<TeaConsumerList>.Instance.GetTopTradeReport(topCount, filter);
            int i = 1;
            foreach (DataRow row in dt.Rows)
            {
                row["Rank"] = i++;//修改名次信息
            }
            #region 增加汇总信息
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.NewRow();
                dr["ItemName"] = string.Format("项目数:{0}", dt.Rows.Count);
                decimal Price = 0M;
                decimal Quantity = 0M;
                decimal OriginalAmount = 0M;
                decimal OfferMoney = 0M;
                decimal Amount = 0M;
                foreach (DataRow row in dt.Rows)
                {
                    Price += Convert.ToDecimal(row["Price"]);
                    Quantity += Convert.ToDecimal(row["Quantity"]);
                    OriginalAmount += Convert.ToDecimal(row["OriginalAmount"]);
                    OfferMoney += Convert.ToDecimal(row["OfferMoney"]);
                    Amount += Convert.ToDecimal(row["Amount"]);
                }
                dr["Price"] = Price;
                dr["Quantity"] = Quantity;
                dr["OriginalAmount"] = OriginalAmount;
                dr["OfferMoney"] = OfferMoney;
                dr["Amount"] = Amount;

                dt.Rows.Add(dt.NewRow());
                dt.Rows.Add(dt.NewRow());
                dt.Rows.Add(dr);
            }
            #endregion
            this.winGridView1.DataSource = dt.DefaultView;
            this.winGridView1.PrintTitle = Portal.gc.gAppUnit + " -- " + "茶室按项目类别统计报表";
        }

        private void BindOpData()
        {
            #region 添加别名解析
            this.winGridView2.AddColumnAlias("Rank", "名次");
            this.winGridView2.AddColumnAlias("ItemName", "项目名称");
            this.winGridView2.AddColumnAlias("Price", "商品单价");
            this.winGridView2.AddColumnAlias("Quantity", "销售数量");
            this.winGridView2.AddColumnAlias("OriginalAmount", "销售总额");
            this.winGridView2.AddColumnAlias("OfferMoney", "优惠金额");
            this.winGridView2.AddColumnAlias("Amount", "折后金额");
            #endregion

            #region 条件检索

            SearchCondition condition = new SearchCondition();
            condition.AddCondition("Creator", this.txtCreator.Text, SqlOperator.Like)
            .AddCondition("BeginTime", this.dtOpStart.Value.ToString(), SqlOperator.MoreThanOrEqual)
            .AddCondition("BeginTime", this.dtOpEnd.Value.ToString(), SqlOperator.LessThanOrEqual);

            string itemInString = "";
            foreach (string item in SelectItemList)
            {
                if (item == "所有类型")
                {
                    itemInString = "";
                    break;
                }
                itemInString += string.Format("'{0}',", item);
            }
            itemInString = itemInString.Trim(',');
            if (!string.IsNullOrEmpty(itemInString))
            {
                condition.AddCondition("ItemType", itemInString, SqlOperator.In);
            }

            string filter = condition.BuildConditionSql(DatabaseType.SqlServer);

            int topCount = Convert.ToInt32(this.txtTopCount.Text);
            #endregion

            DataTable dt = BLLFactory<TeaConsumerList>.Instance.GetTopTradeReport(topCount, filter);
            int i = 1;
            foreach (DataRow row in dt.Rows)
            {
                row["Rank"] = i++;//修改名次信息
            }
            #region 增加汇总信息
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.NewRow();
                dr["ItemName"] = string.Format("项目数:{0}", dt.Rows.Count);
                decimal Price = 0M;
                decimal Quantity = 0M;
                decimal OriginalAmount = 0M;
                decimal OfferMoney = 0M;
                decimal Amount = 0M;
                foreach (DataRow row in dt.Rows)
                {
                    Price += Convert.ToDecimal(row["Price"]);
                    Quantity += Convert.ToDecimal(row["Quantity"]);
                    OriginalAmount += Convert.ToDecimal(row["OriginalAmount"]);
                    OfferMoney += Convert.ToDecimal(row["OfferMoney"]);
                    Amount += Convert.ToDecimal(row["Amount"]);
                }
                dr["Price"] = Price;
                dr["Quantity"] = Quantity;
                dr["OriginalAmount"] = OriginalAmount;
                dr["OfferMoney"] = OfferMoney;
                dr["Amount"] = Amount;

                dt.Rows.Add(dt.NewRow());
                dt.Rows.Add(dt.NewRow());
                dt.Rows.Add(dr);
            }
            #endregion
            this.winGridView2.DataSource = dt.DefaultView;
            this.winGridView2.PrintTitle = Portal.gc.gAppUnit + " -- " + "茶室按操作员统计报表";
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            BindItemCateData();
        }

        private void btnOpSearch_Click(object sender, EventArgs e)
        {
            BindOpData();
        }

        private void btnOpSelectItems_Click(object sender, EventArgs e)
        {
            FrmStatisticItems dlg = new FrmStatisticItems();
            dlg.cbxItems.Items.Clear();
            List<string> itemList = BLLFactory<TeaConsumerList>.Instance.GetGoodsType();
            dlg.cbxItems.Items.AddRange(itemList.ToArray());
            dlg.cbxItems.Items.Insert(0, "所有类型");

            dlg.ItemList = SelectItemList;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                SelectItemList = new List<string>();
                for (int i = 0; i < dlg.cbxItems.CheckedItems.Count; i++)
                {
                    string item = dlg.cbxItems.GetItemText(dlg.cbxItems.CheckedItems[i]);
                    if (!SelectItemList.Contains(item))
                    {
                        SelectItemList.Add(item);
                    }
                }
                BindOpData();
            }
        }

        private void menu_InRefresh_Click(object sender, EventArgs e)
        {
            BindItemCateData();
        }

        private void menu_OutRefresh_Click(object sender, EventArgs e)
        {
            BindOpData(); 
        }

    }
}
